-- blacksmith
-- created by panyl
-- 地牢格子：工坊

return {
    -- 自动战斗
    autoFight = function(grid)
        -- 格子数据
        local pos = grid:getPos();
        local gridData = DungeonM.getGridByPos(pos);
        local elemId = gridData.element;
        local elemData = DungeonM.queryElement(elemId, "dbase");
        local upgradeStyle = elemData.upgrade_style;

        -- 获取玩家当前所有宝物
        local equips = table.deepcopy(EquipM.getUserTreasureList());
        local list = {};
        for classId, amount in pairs(equips) do
            table.insert(list, classId);
        end

        -- 随机一下数组
        for oriIndex, classId in pairs(list) do
            local toIndex = math.random(#list);
            local temp = list[toIndex];
            list[toIndex] = list[oriIndex];
            list[oriIndex] = temp;
        end

        for _, classId in pairs(list) do
            local isEquiped = false;
            if EquipM.isEquiped(classId) then
                isEquiped = true;
            end

            local ok;
            if isEquiped then
                -- 已装备的
                ok = DungeonActionM.go("upgrade_wear_equip", pos, classId);
            else
                -- 尚未装备的
                ok = DungeonActionM.go("upgrade_equip", pos, classId);
            end

            if ok then
                return true;
            end
        end

        return false;
    end,
};
